package hkust.csit5110.android.util;

import hkust.lingx.csit5110.android.lib.model.AnchorMessage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Application;

/**
 * 
 * Global Message History for each matcher, simply cache in memory
 * restart the app will lost history.
 *
 */
public class GlobalChatListMap extends Application {

  public Map<String,List<AnchorMessage>> ipChatHist=new HashMap<String,List<AnchorMessage>>();
  
  public void addChatContent(String ip,String content){
    List<AnchorMessage> hist=ipChatHist.get(ip);
    if(hist==null){
      hist=new ArrayList<AnchorMessage>();
      ipChatHist.put(ip, hist);
    }
    AnchorMessage anchor=new AnchorMessage();
    anchor.setMsgFrom(ip);
    anchor.setMsgContent(content);
    hist.add(anchor);
  }
  
  public void addAnchorMessage(String ip,AnchorMessage anchor){
    List<AnchorMessage> hist=ipChatHist.get(ip);
    if(hist==null){
      hist=new ArrayList<AnchorMessage>();
      ipChatHist.put(ip, hist);
    }
    hist.add(anchor);
  }
  
  public Object[] getChatList(String ip){
    List<AnchorMessage> hist=ipChatHist.get(ip);
    List<String> strList=new ArrayList<String>();
    if(hist==null){
      hist=new ArrayList<AnchorMessage>();
      ipChatHist.put(ip, hist);
    }
    for(AnchorMessage anchor:hist){
      StringBuffer msg=new StringBuffer();
      if(AndConstants.getLocalIpAddress().equals(anchor.getMsgFrom())){
        msg.append("I said:");
      }else{
        msg.append("Stranger said:");
      }
      msg.append(anchor.getMsgContent());
      strList.add(msg.toString());
    }
    return strList.toArray();
  }
  
 
}
